'use strict';

/******************************************************************************
Copyright (c) Microsoft Corporation.

Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
***************************************************************************** */
/* global Reflect, Promise, SuppressedError, Symbol */


var __assign = function() {
    __assign = Object.assign || function __assign(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};

typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
    var e = new Error(message);
    return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
};

/**
 * 将Blob转换为File对象
 * @param {Blob} blob - 需要转换的Blob对象
 * @param {string} fileName - 转换后的文件名
 * @returns {File} 返回转换后的File对象
 */
var blobToFile = function (blob, fileName) {
    return new File([blob], fileName, { type: blob.type });
};
var compressPicture = function (file, option) {
    return new Promise(function (resolve, reject) {
        var opt = __assign({ compressMaxNum: 3, quality: 0.8, maxWidth: 800, maxSize: 1024 }, option);
        var compressNum = 0;
        // 压缩
        var _compress = function (file) {
            // 创建FileReader对象来读取文件
            var reader = new FileReader();
            reader.onload = function (e) {
                var img = new Image();
                img.onload = function () {
                    // 创建canvas元素
                    var canvas = document.createElement("canvas");
                    var ctx = canvas.getContext("2d");
                    // 计算压缩比例
                    var MAX_WIDTH = opt.maxWidth ? opt.maxWidth : img.width;
                    var width = img.width;
                    var height = img.height;
                    if (width > height) {
                        if (width > MAX_WIDTH) {
                            height *= MAX_WIDTH / width;
                            width = MAX_WIDTH;
                        }
                    }
                    else {
                        if (height > MAX_WIDTH) {
                            width *= MAX_WIDTH / height;
                            height = MAX_WIDTH;
                        }
                    }
                    // 设置canvas尺寸
                    canvas.width = width;
                    canvas.height = height;
                    // 绘制图片并压缩
                    ctx.drawImage(img, 0, 0, width, height);
                    canvas.toBlob(function (blob) {
                        var size = (blob === null || blob === void 0 ? void 0 : blob.size) / 1024;
                        compressNum++;
                        console.log("[".concat(file.name, "] \u7B2C").concat(compressNum, "\u6B21\u538B\u7F29,\u538B\u7F29\u524D:").concat(file.size / 1024, "kb, \u538B\u7F29\u540E:").concat(size, "kb"));
                        var _file = blobToFile(blob, file.name);
                        if (opt.maxSize !== -1 && size > opt.maxSize && compressNum <= opt.compressMaxNum) {
                            return _compress(_file);
                        }
                        else {
                            var base64 = canvas.toDataURL(file.type, opt.quality);
                            var _fileObj = {
                                filename: file.name,
                                size: file.size,
                                file: _file,
                                blob: blob,
                                base64: base64,
                            };
                            resolve(_fileObj);
                        }
                    }, file.type, 0.8);
                };
                img.onerror = function (e) {
                    reject(e);
                };
                img.src = e.target.result;
            };
            reader.onerror = function (e) {
                reject(e);
            };
            // 读取文件为DataURL
            reader.readAsDataURL(file);
        };
        _compress(file);
    });
};
var random = function (min, max) {
    return Math.floor(Math.random() * (max - min + 1)) + min;
};

exports.compressPicture = compressPicture;
exports.random = random;
